#! /usr/bin/env python

def best(lst,number):
    """Return a descending sorted list based on passed list & number"""
    
    result = []
    if len(lst) == number:
        result = sorted(lst)
    elif number == 1:
        result.append(sorted(lst)[len(lst) - 1])
    else:
        for x in range(len(lst) - 1, len(lst) - number, -1): # find best number
            result.append(sorted(lst)[x])

    # debug print 'best result', result

    return result
    
def qual(units, B, C):
    """Return the number of points for a qualication"""
    # pass list of units and their grade points plus 
    # the combo of group B and group C units
    # there must always be a group A unit
    
    result = 0
    group_a = 1
    group_c = 19
    result_a = 0
    result_b = 0
    result_c = 0
    
    # get group A result
    result_a = units[1]
    
    if result_a: # only continue if group A is Pass or above
        
        # check if enough group B results qualify
        # count Bs
        count = 0
        points = []
        for x in range(0,len(units),2): # step thro each unit
            if units[x] > group_a and units[x] < group_c  :
                if units[x + 1]: 
                    count = count +1
                    points.append(units[x + 1]) # create B points list
        # check if enough group B results qualify
        if count >= B:
            # create new (best) group B list 
            use = []
            use = best(points, B)
            # get group B result
            for x in range(len(use)): 
                result_b = result_b + use[x]
    
        # check if enough group C results qualify
        # count Cs
        count = 0
        points = []
        for x in range(0,len(units),2): # step thro each unit
            if units[x] > group_c  :
                if units[x + 1]: 
                    count = count +1
                    points.append(units[x + 1]) # create C points list
        # check if enough group C results qualify
        if count >= C:
            # create new (best) group C list 
            use = []
            use = best(points, C)
            # get group C result
            for x in range(len(use)): 
                result_c = result_c + use[x]

    # check results are non-zero when B or C are non-zero
    if result_a and (result_b or not B) and (result_c or not C):
        # sum group A, B and C results
        result = result_a + result_b + result_c
    # debug
    # print '---->', result_a, result_b, result_c, result
        
    return result
    


def grading(units):
    """Return a list of possible overall grades for IT Nationals"""

    group_a = 1
    group_c = 19
    result_fa = 0
    result_a = 0
    result_fc = 0
    result_c = 0
    points = []
    result = []
    
    # have a dummy answer result first
    grades = ['first award','award','first certificate', 'certificate']
    print
    print 'qualification structure' 
    for gcse in range(len(grades)):
        print gcse + 1, grades[gcse]
    print
    
    
    # echo units
    print 'units'
    for unit in range(0,len(units),2):
        print units[unit], units[unit + 1]
    print
    
    # swap grades for points
    for unit in range(0,len(units),2):
        if units[unit + 1] == 'P': units[unit + 1] = 1
        if units[unit + 1] == 'M': units[unit + 1] = 2
        if units[unit + 1] == 'D': units[unit + 1] = 3
        if units[unit + 1] == 'F': units[unit + 1] = 0
        if units[unit] >= 19: units[unit + 1] = units[unit + 1] * 0.5

    # echo units
    print 'units'
    for unit in range(0,len(units),2):
        print units[unit], units[unit + 1]
    print
                    

    # group A result -> exit if 'F'
    if units[1]: # only continue if group A is Pass or above
        
        # First Award = A + C              ***
        result_fa = qual(units, 0, 1)
        # Award = A + 2B                   ***
        # Award = A + B + 2C               ***
        if qual(units, 2, 0) > qual(units, 1, 2):
            result_a = qual(units, 2, 0)
        else:
            result_a = qual(units, 1, 2)
        # First Certificate = A + 3B + C   ***
        # First Certificate = A + 2B + 3C
        if qual(units, 3, 1) > qual(units, 2, 3):
            result_fc = qual(units, 3, 1)
        else:
            result_fc = qual(units, 2, 3)
        # Certificate = A + 5B
        # Certificate = A + 4B + 2C        ***
        # Certificate = A + 3B + 4C
        if qual(units, 5, 0) > qual(units, 4, 2):
            result_c = qual(units, 5, 0)
        elif qual(units, 4, 2) > qual(units, 3, 4):
            result_c = qual(units, 4, 2)
        else:
            result_c = qual(units, 3, 4)
            
    result.append(result_fa)
    result.append(result_a)
    result.append(result_fc)
    result.append(result_c)
    
    return result
    
test = grading([1,'P', 2, 'P', 4, 'M', 13, 'F', 20, 'P', 23, 'D'])
print
print 'result:', test
print
